﻿/*
 * Класс для конвертации представлений изображений (в массив и обратно)
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace DiplomNN
{
    class Colors
    {
        public unsafe static byte[, ,] BitmapToByteRgb(Bitmap bmp)
        {
            int width = bmp.Width,
                height = bmp.Height;
            byte[, ,] res = new byte[3, height, width];
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly,
                PixelFormat.Format24bppRgb);
            try
            {
                byte* curpos;
                for (int h = 0; h < height; h++)
                {
                    curpos = ((byte*)bd.Scan0) + h * bd.Stride;
                    for (int w = 0; w < width; w++)
                    {
                        res[2, h, w] = *(curpos++);
                        res[1, h, w] = *(curpos++);
                        res[0, h, w] = *(curpos++);
                    }
                }
            }
            finally
            {
                bmp.UnlockBits(bd);
            }
            return res;
        }

        public unsafe static Bitmap RgbToBitmap(byte[, ,] rgb)
        {
            if ((rgb.GetLength(0) != 3))
            {
                throw new ArrayTypeMismatchException("Size of first dimension for passed array must be 3 (RGB components)");
            }

            int width = rgb.GetLength(2),
                height = rgb.GetLength(1);

            Bitmap result = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            BitmapData bd = result.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
                PixelFormat.Format24bppRgb);

            try
            {
                byte* curpos;
                for (int h = 0; h < height; h++)
                {
                    curpos = ((byte*)bd.Scan0) + h * bd.Stride;
                    for (int w = 0; w < width; w++)
                    {
                        *(curpos++) = rgb[2, h, w];
                        *(curpos++) = rgb[1, h, w];
                        *(curpos++) = rgb[0, h, w];
                    }
                }
            }
            finally
            {
                result.UnlockBits(bd);
            }

            return result;
        }

        public static byte[, ,] ByteRgbToBlackWhiteByteRgb(byte[, ,] srcImage)
        {
            byte[, ,] DstImage = new byte[srcImage.GetLength(0), srcImage.GetLength(1), srcImage.GetLength(2)];
            for (int i = 0; i < srcImage.GetLength(1); i++)
            {
                for (int j = 0; j < srcImage.GetLength(2); j++)
                {
                    if ((srcImage[0, i, j] != 255) && (srcImage[1, i, j] != 255) && (srcImage[2, i, j] != 255))
                    {
                        DstImage[0, i, j] = 0;
                        DstImage[1, i, j] = 0;
                        DstImage[2, i, j] = 0;
                    }
                    else
                    {
                        DstImage[0, i, j] = 255;
                        DstImage[1, i, j] = 255;
                        DstImage[2, i, j] = 255;
                    }
                } 
            }

            return DstImage;
        }
    }
}
